package jtk.project4.fleet.screen.generalTankInformation;

import java.awt.Choice;
import java.awt.FlowLayout;
import java.util.ArrayList;

import jtk.project4.fleet.action.AddNewFuelTankAction;
import jtk.project4.fleet.domain.FuelRec;
import jtk.project4.fleet.event.GeneralTankInformationEvent;
import jtk.project4.fleet.field.AddEditDeleteField;
import jtk.project4.fleet.field.AddEditDeleteInventoryReceiptField;
import jtk.project4.fleet.field.GeneralTankInformationField;
import jtk.project4.fleet.field.InventoryDetailsField;
import jtk.project4.fleet.field.InventoryReceiptField;

import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.control.TabControl;
import nl.coderight.jazz.form.control.TabItem;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.PushButton;

public class GeneralTankInformationView extends FormView
{
	private TabControl FuelInventoryTab;
	private TabItem GeneralTankInformationTab;
	private TabItem InventoryReceiptsTab;
	
	private FormHeader formHeader;
	private PushButton submitButton;
	private PushButton cancelButton;
	private PushButton addFuelTank;
	private Action addFuelTankAction;
	private TableField<FuelRec> fuelrecTable;
	
	protected void init() 
	{
		createTabFields();
		createFields();
		createLayout();		
	}
	
	private void createTabFields()
	{
		FuelInventoryTab=new TabControl();
		GeneralTankInformationTab=new TabItem("GeneralTankInformation");
		InventoryReceiptsTab=new TabItem("InventoryReceipts");
	}
	
	private FormField createTabs()
	{
		TabControl form=FuelInventoryTab;
		form.addTab(GeneralTankInformationTab,createGeneralTankInformationTab());
		form.addTab(InventoryReceiptsTab, createInventoryReceiptTab());
		return form;
	}
	
	private GroupControl createGeneralTankInformationTab()
	{
		GroupControl form=new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new GeneralTankInformationField("GeneralTankInformation"))
		.addRow()
		.addField(new InventoryDetailsField("InventoryDetails"));
		return form;
	}
	
	private GroupControl createInventoryReceiptTab()
	{
		GroupControl form=new GroupControl();
		form.setLayout(new FormLayout())
		//.addField(new InventoryReceiptField("inventoryreceipt"))
		.addField(fuelrecTable)
		.addRow()
		.addField(new AddEditDeleteInventoryReceiptField("aaa"));
		return form;
	}
	
	
	
	private void createFields()
	{
		//setup table
		fuelrecTable = new TableField<FuelRec>("fuelrecs","fuelrec");
		fuelrecTable.setAutoResizeMode(AutoResizeMode.ALL_COLUMNS);
		fuelrecTable.getSelectionValues();
		
		//add columns
		fuelrecTable.addColumn("label.qtyrcvd","qty");
		fuelrecTable.addColumn("label.unitcost","unitCost");
		fuelrecTable.addColumn("label.datercvd","createDate");
		fuelrecTable.addColumn("label.vendor","vendorId");
		
		submitButton = new PushButton("save");
		cancelButton = new PushButton("cancel");
	}
	
	
	private void createLayout() 
	{
		setHeader(createHeader());
		addField(createForm());
	}
	
	private FormHeader createHeader() 
	{
		formHeader = new FormHeader();
		formHeader.setTitle("General Tank Information.header.form");
		return formHeader;
	}
	
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		form.addField(createTabs(), CENTER);
		form.addField(createLeftField(), WEST);
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	private GroupControl createLeftField() {
		GroupControl field = new GroupControl();
		field.setLayout(new FlowLayout(FlowLayout.CENTER));
		addFuelTankAction = new AddNewFuelTankAction();
		field.addField(addFuelTank = new PushButton(addFuelTankAction), SOUTH);
		return field;
	}
	
	public void handleEvent (GeneralTankInformationEvent evt){
		FuelRec selectedFuelRec = fuelrecTable.getValue();
		switch(evt.getType()){
		case CANCEL:
			close();
		/*case CHANGE:
			//bwt filter
		case SELECT :
			ArrayList<Choice> choices = new ArrayList<Choice>();
			choices.add(new Choice(0, "status.message.none"));
			choices.add(new Choice(1, "status.message.one"));
			choices.add(new Choice(2, "status.message.all"));
		break;
		case DELETE:
			propagateEvent(evt);

			break;*/
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		

		}
	}
}
